package cn.xs.user.dao.impl;


import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.xs.core.dao.impl.BaseDaoImpl;
import cn.xs.core.util.TimeUtils;
import cn.xs.user.dao.UserDao;
import cn.xs.user.entity.Sign;
import cn.xs.user.entity.User;
import cn.xs.user.entity.UserLuckDraw;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public void save(User entity) {
		try{
			String sql="insert into t_user values(?,?,?,?,?);";
			Object[] params = {entity.getId(),entity.getAccount(),entity.getPassword(),entity.getEmail(),entity.getPhone()};
			getQueryRunner().update(sql,params);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public User findUserByAccAndPwd(String account, String password) {
		try{
			String sql = "select * from t_user where account = ? and password = ?;";
			Object[] params = {account,password};
			return (User) getQueryRunner().query(sql, new BeanHandler(User.class), params);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void addLockCount(String uid) {
		try{
			UserLuckDraw ld = findLockCount(uid);
			//如果没有记录，则新增一条记录，抽奖记录为1
			if(ld==null){
				String sql = "insert into t_user_luck_draw values(?,?)";
				Object[] params = {uid,1};
				getQueryRunner().update(sql, params);
			}else{
				//有记录则在原来基础上加1
				String sql = "update t_user_luck_draw set luckcount = ? where uid=?";
				Object[] params = {ld.getLuckcount()+1,uid};
				getQueryRunner().update(sql, params);
			}
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public UserLuckDraw findLockCount(String uid) {
		try{
			String sql = "select * from t_user_luck_draw where uid= '"+uid+"'";
			return (UserLuckDraw) getQueryRunner().query(sql, new BeanHandler(UserLuckDraw.class));
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void deleteLockCount(String uid) {
		try{
			UserLuckDraw ld = findLockCount(uid);
			//如果没有记录，则新增一条记录，抽奖记录为1
			if(ld!=null && ld.getLuckcount()>0){
				//有记录则在原来基础上加1
				String sql = "update t_user_luck_draw set luckcount = ? where uid=?";
				Object[] params = {ld.getLuckcount()-1,uid};
				getQueryRunner().update(sql, params);
			}
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void sign(String uid) {
		try{
			String sql = "insert into t_sign values(?,?)";
			Object[] params = {uid,new Date()};
			getQueryRunner().update(sql, params);
		}catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@Override
	public Sign checkSign(String uid,String time) {
		 try{
			 String sql = "select * from t_sign where time = ? and uid = ?";
			 Object[] params = {time,uid};
			return (Sign) getQueryRunner().query(sql, new BeanHandler(Sign.class), params);
		 }catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@Override
	public List<Sign> getSignCount(String uid) {
		try{
			TimeUtils tu = new TimeUtils();
			String sql = "select * from t_sign where ? <=time and time <=? and uid = ?";
			Object[] params = {tu.getMonthTime(),tu.getLastMonthTime(),uid};
			return (List<Sign>) getQueryRunner().query(sql, new BeanListHandler(Sign.class), params);
		}catch (Exception e) {
			throw new RuntimeException();
		}
	}
}
